<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../01.getting-started/demo.css">
    <title>综合示例</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>

<div id="app">
    <div id="searchBar">
        搜索 <input type="text" v-model="searchQuery"/>
    </div>
    <grid :data="gridData" :columns="gridColumns" :filter-key="searchQuery"></grid>
</div>

<template id="grid">
    <table>
        <thead>
        <tr>
            <th v-for="c in columns">
                {{ c | capitalize }}
            </th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="entry in filteredData">
            <td v-for="c in columns">
                {{entry[c]}}
            </td>
        </tr>
        </tbody>
    </table>
</template>

<script>

    var grid = Vue.component('grid', {
        template: '#grid',
        props: {
            data: Array,
            columns: Array,
            filterKey: String
        },
        computed: {
            filteredData: function () {
                if (!this.filterKey) return this.data;
                let newArray = [];
                for (let p of this.data) {
                    if (!p.name.indexOf(this.filterKey)) {
                        newArray.push(p);
                    }
                }
                return newArray;
            }
        },
        filters: {
            capitalize: function (value) {
                if (!value) return '';
                value = value.toString();
                return value.charAt(0).toUpperCase() + value.slice(1)
            }
        }
    });

    new Vue({
        el: '#app',
        data: {
            searchQuery: '',
            gridColumns: ['name', 'age', 'sex'],
            gridData: [{
                name: 'Jack',
                age: 30,
                sex: 'Male'
            }, {
                name: 'Bill',
                age: 26,
                sex: 'Male'
            }, {
                name: 'Tracy',
                age: 22,
                sex: 'Female'
            }, {
                name: 'Chris',
                age: 36,
                sex: 'Male'
            }]
        }
    });
</script>
</body>
</html>